package com.example.pool;

import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;

/**
 * sftp池化工厂
 */
public class SftpProcessFactory implements PooledObjectFactory<SftpConnections> {

    /**
     * 重新初始化池要返回的实例
     */
    @Override
    public void activateObject(PooledObject<SftpConnections> pooledObject) throws Exception {

    }

    /**
     * 销毁实例
     */
    @Override
    public void destroyObject(PooledObject<SftpConnections> pooledObject) throws Exception {
        System.out.println("销毁对象");
    }

    /**
     * 创建对象
     */
    @Override
    public PooledObject<SftpConnections> makeObject() throws Exception {
        // 创建对象的逻辑 返回一个池中的包装类
        return new DefaultPooledObject<>(new SftpConnections());
    }

    /**
     * 取消初始化实例对应activateObject方法
     */
    @Override
    public void passivateObject(PooledObject<SftpConnections> pooledObject) throws Exception {

    }

    /**
     * 校验实例对象
     */
    @Override
    public boolean validateObject(PooledObject<SftpConnections> pooledObject) {
        // 验证对象 默认不校验，如果需要校验在PooledObjectFactory配置中进行配置
        return true;
    }
}
